package com.quasar.hpatient.view.friends;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.medicalmodel.FriendsData;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.presenter.FriendsListPresenter;
import com.quasar.hpatient.view.adapter.FriendsListAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;

import chatui.ui.ChatActivity;

/**
 * 好友列表
 */
@OptionsMenu(R.menu.menu_add_friend)
@EActivity(R.layout.activity_friends_list)
public class FriendsListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener,
        AnotherResultListener<AnotherResult<FriendsData>> {

    public  FriendsListAdapter friendsListAdapter;
    private int delayMillis = 1000;//设置刷新等待的时间
    private FriendsListPresenter friendsListPresenter;
    //设置分页
    private int page=1;
    //判断下一页是否存在数据
    private boolean onLoadMore=false;

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;

    @AfterViews
    void init(){
        initView();
        initData();
    }
    @Override
    protected void initView() {
        initToolbar();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        friendsListPresenter=new FriendsListPresenter(this);
        initAdapter();
    }

    //初始化adapter布局
    public void initAdapter() {
        friendsListAdapter = new FriendsListAdapter();
        friendsListAdapter.setOnLoadMoreListener(this, rv_list);
        friendsListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(friendsListAdapter);
        //初始化数据
        friendsListPresenter.PostFriendsList(page+"");
        //item点击
        friendsListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.ll_frient_item:
                        //进入好友聊天界面
                        FriendsListActivity.this.startActivity(new Intent(FriendsListActivity.this, ChatActivity.class)
                                .putExtra("userId",friendsListAdapter.getItem(position).getBmobUserName()));
                        break;
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        page=1;
        friendsListPresenter.PostFriendsList(page+"");
        friendsListAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                friendsListAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {

        page++;//下拉时page递增
        friendsListPresenter.PostFriendsList(page+"");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    friendsListAdapter.loadMoreEnd();
                } else {
                    friendsListAdapter.loadMoreComplete();
                    friendsListAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }

    //添加好友
    @OptionsItem(R.id.menu_addFriend)
    public void menu_addFriend(){
        AddFriendsActivity_.intent(this).start();
    }

    @Override
    public void onSuccess(AnotherResult<FriendsData> data) {
        //判断page是否是下拉还是上拉
        if(page==1){
            friendsListAdapter.setNewData(data.getList());
            onLoadMore=false;
        }else{
            friendsListAdapter.addData(data.getList());
            if(data.getList().size()==0){
                onLoadMore=true;
            }
        }
    }

    @Override
    public void onError(String msg) {
        if(msg!=null){
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        friendsListAdapter.notifyDataSetChanged();
    }
}
